package main

import (
	ui "github.com/gizak/termui/v3"
	"github.com/gizak/termui/v3/widgets"
	"log"
)

type nodeValue string

func (nv nodeValue) String() string {
	return string(nv)
}

func main() {
	if err := ui.Init(); err != nil {
		log.Fatalf("failed to initialize termui: %v", err)
	}
	defer ui.Close()

	table := widgets.NewList()
	table.Title = "List"
	table.Rows = []string{
		"[0] github.com/gizak/termui/v3",
		"[1] [你好，世界](fg:blue)",
		"[2] [こんにちは世界](fg:red)",
		"[3] [color](fg:white,bg:green) output",
		"[4] output.go",
		"[5] random_out.go",
		"[6] dashboard.go",
		"[7] foo",
		"[8] bar",
		"[9] baz",
	}
	table.TextStyle = ui.NewStyle(ui.ColorYellow)
	table.WrapText = true
	//l.SetRect(0, 0, 25, 8)

	nodes := []*widgets.TreeNode{
		{
			Value: nodeValue("Key 1"),
			Nodes: []*widgets.TreeNode{
				{
					Value: nodeValue("Key 1.1"),
					Nodes: []*widgets.TreeNode{
						{
							Value: nodeValue("Key 1.1.1"),
							Nodes: nil,
						},
						{
							Value: nodeValue("Key 1.1.2"),
							Nodes: nil,
						},
					},
				},
				{
					Value: nodeValue("Key 1.2"),
					Nodes: nil,
				},
			},
		},
		{
			Value: nodeValue("Key 2"),
			Nodes: []*widgets.TreeNode{
				{
					Value: nodeValue("Key 2.1"),
					Nodes: nil,
				},
				{
					Value: nodeValue("Key 2.2"),
					Nodes: nil,
				},
				{
					Value: nodeValue("Key 2.3"),
					Nodes: nil,
				},
			},
		},
		{
			Value: nodeValue("Key 3"),
			Nodes: nil,
		},
	}
	list := widgets.NewTree()
	list.TextStyle = ui.NewStyle(ui.ColorYellow )
	list.WrapText = false
	list.SetNodes(nodes)

	title := widgets.NewParagraph()
	title.Text = "<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget"
	title.Title = "Home"
	//
	//table := widgets.NewParagraph()
	//table.Text = "<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget"
	//table.Title = "Table"
	//
	//list := widgets.NewParagraph()
	//list.Text = "<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget"
	//list.Title = "List"


	grid := ui.NewGrid()
	termWidth, termHeight := ui.TerminalDimensions()
	grid.SetRect(0, 0, termWidth, termHeight)

	grid.Set(
		ui.NewRow(1.0/10,
			ui.NewCol(1.0, title),
		),
		ui.NewRow(9.0/10,
			ui.NewCol(1.5/10, table),
			ui.NewCol(8.5/10, list),
		),
	)

	ui.Render(grid)
	previousKey := ""
	uiEvents := ui.PollEvents()
	for {
		select {
		case e := <-uiEvents:
			switch e.ID {
			case "<Resize>":
				payload := e.Payload.(ui.Resize)
				grid.SetRect(0, 0, payload.Width, payload.Height)
				ui.Clear()
				ui.Render(grid)
			case "q", "<C-c>":
				return
			case "j", "<Down>":
				table.ScrollDown()
			case "k", "<Up>":
				table.ScrollUp()
			case "<C-d>":
				table.ScrollHalfPageDown()
			case "<C-u>":
				table.ScrollHalfPageUp()
			case "<C-f>":
				table.ScrollPageDown()
			case "<C-b>":
				table.ScrollPageUp()
			case "g":
				if previousKey == "g" {
					table.ScrollTop()
				}
			case "<Home>":
				table.ScrollTop()
			case "G", "<End>":
				table.ScrollBottom()
			}
		}
	}
}